package com.fita.admin.service.data.impl;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.page.Page;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.persistence.support.QueryFilter;
import com.fita.admin.dao.VehicleParameterDao;
import com.fita.admin.service.DataServiceImpl;
import com.fita.admin.service.data.VehicleParameterService;
import com.fita.entity.data.VehicleParameter;
import org.apache.commons.lang3.ClassUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 
 * @author cuteHU
 *
 */
@Service("vehicleParameterService")
public class VehicleParameterServiceImpl extends DataServiceImpl<VehicleParameter> implements VehicleParameterService {

    @Dao
    private VehicleParameterDao vehicleParameterDao;

    @Autowired
    private GenericDaoSupport genericDaoSupport;

    /*
     * (non-Javadoc)
     * 
     * @see com.fita.admin.service.data.VehicleParameterService#count()
     */
    public int count() {
        return vehicleParameterDao.count();
    }

    /* (non-Javadoc)
     * @see com.fita.admin.service.DataServiceImpl#listByPage(java.lang.Class, com.demo2do.core.entity.page.Page)
     */
    @SuppressWarnings("unchecked")
    public List<VehicleParameter> listByPage(Class<VehicleParameter> clazz, Page page) {
        List<VehicleParameter> result = genericDaoSupport.searchForList("FROM " + ClassUtils.getShortClassName(clazz) + " WHERE 1 = 1", page.getBeginIndex(), page.getEveryPage());
        page.calculate();
        return result;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.fita.admin.service.data.VehicleParameterService#countSearch(java.lang.
     * Class, com.demo2do.core.persistence.support.QueryFilter)
     */
    public int countSearch(Class<VehicleParameter> clazz, QueryFilter queryFilter) {
        return genericDaoSupport.searchForInt(
                "SELECT COUNT(*) FROM " + ClassUtils.getShortClassName(clazz) + queryFilter.whereSentence(),
                queryFilter.parameters());
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.fita.admin.service.data.VehicleParameterService#createVehicleParameter(
     * com.fita.entity.data.VehicleParameter)
     */
    public void createVehicleParameter(VehicleParameter vehicleParameter) {
        VehicleParameter vehicleParameter1 = vehicleParameterDao.load(vehicleParameter.getEnglishName());
        if (vehicleParameter1 != null) {
            throw BusinessException.newInstance("400", "新增车辆参数已存在");
        }
        genericDaoSupport.save(vehicleParameter.initliaze());
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.fita.admin.service.data.VehicleParameterService#activate(com.fita.entity.
     * data.VehicleParameter)
     */
    public void activate(VehicleParameter vehicleParameter) {
        vehicleParameter.activate();
        genericDaoSupport.update(vehicleParameter);

    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.fita.admin.service.data.VehicleParameterService#freeze(com.fita.entity.
     * data.VehicleParameter)
     */
    public void freeze(VehicleParameter vehicleParameter) {
        vehicleParameter.freeze();
        genericDaoSupport.update(vehicleParameter);

    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.fita.admin.service.data.VehicleParameterService#update(java.lang.Long,
     * com.fita.entity.data.VehicleParameter)
     */
    public void update(Long id, VehicleParameter vehicleParameter) {
        List<VehicleParameter> loads = vehicleParameterDao.loads(vehicleParameter.getEnglishName());
        VehicleParameter load = genericDaoSupport.load(VehicleParameter.class, id);
        // 如果车辆参数英文名称没有修改，更新
        if (vehicleParameter.getEnglishName().equals(load.getEnglishName())) {
            genericDaoSupport.update(load.edit(vehicleParameter));
        } else if (loads.size() > 0) {
            throw BusinessException.newInstance("400", "车辆参数英文名称不可重复！");
        }
    }

}
